package com.kingsalert.kingsalert.view.process_widget;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.kingsalert.kingsalert.R;
import com.kingsalert.kingsalert.adapter.ProcessInfoInputListAdapter;
import com.kingsalert.kingsalert.base.BaseProcessWidgetManager;
import com.kingsalert.kingsalert.bean.process.ProcessInfo_Data;
import com.kingsalert.kingsalert.util.http.HttpUtil;

import java.util.ArrayList;
import java.util.Arrays;

public class SelectViewManager extends BaseProcessWidgetManager {

    private TextView titleTv;
    private TextView inputTv;
    private View listLayout;
    private ListView listLv;

    private int itemHeight = 0;
    private ArrayList<String> items;

    public SelectViewManager(View view, ProcessInfo_Data data,String page){
        super(view,data,page);
    }

    @Override
    protected void init(){

        titleTv = mView.findViewById(R.id.vps_tv_title);
        View clickLayout = mView.findViewById(R.id.vps_view_click);
        inputTv = mView.findViewById(R.id.vps_tv_input);
        listLayout = mView.findViewById(R.id.vps_view_list);
        listLv = mView.findViewById(R.id.vps_lv_list);

        setTitle(mData.getTitle());
        inputTv.setHint(mData.getHint());

        clickLayout.setOnClickListener(v -> {
            listLayout.setVisibility(listLayout.getVisibility()==VISIBLE ? GONE:VISIBLE);
            HttpUtil.getInstance().saveBuriedPoint(page,mData.getParamName().toUpperCase()+"_CLICK","");
        });

        addItems(mData.getTitlesOfValue());
    }

    @Override
    public boolean checkInput() {
        if (!mData.isMustInput()) return true;
        return !TextUtils.isEmpty(inputTv.getText());
    }

    @Override
    public String getInput() {
        if (mData == null) return "";
        return mData.getValueOfTitle(inputTv.getText().toString());
    }

    public String getTextInput(){
        return inputTv.getText().toString();
    }

    public void setTitle(String title){
        if (TextUtils.isEmpty(title)) titleTv.setVisibility(GONE);
        else {
            titleTv.setVisibility(VISIBLE);
            titleTv.setText(title);
        }
    }

    public void addItems(ArrayList<String> itemList){
        items = itemList;
        ProcessInfoInputListAdapter listAdapter = new ProcessInfoInputListAdapter(mView.getContext(), items);
        listLv.setAdapter(listAdapter);
        listLv.setOnItemClickListener((parent, view, position, id) -> {
            if (items ==null) return;
            String input = items.get(position);

            HttpUtil.getInstance().saveBuriedPoint(page,mData.getParamName().toUpperCase()+"_CLICK",input);

            inputTv.setText(input);
            listLayout.setVisibility(GONE);
        });

        listAdapter.notifyDataSetChanged();

        ViewGroup.LayoutParams layoutParams = listLv.getLayoutParams();
        if (itemHeight ==0) itemHeight = layoutParams.height;
        layoutParams.height = items.size()* itemHeight;
        listLv.setLayoutParams(layoutParams);

    }

    public void addItems(String[] itemArr){
        addItems(new ArrayList<>(Arrays.asList(itemArr)));
    }

}
